home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / 80x0393.zip / UART_TUT.FAQ < prev    next >
Text File  |  1993-03-30  |  27KB  |  593 lines

  1.                       The Serial Hardware Tutorial
  2.                 By Yousuf J. Khan (FidoNet 1:163/215.4)
  3.                          Revision: Jan 20, 1993
  4.  
  5. Serial communications on the IBM PC and compatible range of computers
  6. conforms to the RS-232C serial port standard. The chip that implements
  7. and controls the RS-232 serial port is the UART.
  8.  
  9. BIOS Data Segment
  10. -----------------
  11. The base port addresses of each UART can be pointed to by reading the
  12. four memory words at offset 0, 2, 4, and 6 from the start of the BIOS
  13. Data Segment (BDS), segment 40h. Each word points to the starting port
  14. address of a UART assembly. These pointer words are known commonly as
  15. COM1, COM2, COM3, and COM4.
  16.  
  17. Some standard base port locations to put the UART assemblies are at
  18. ports 02E8h, 02F8h, 03E8h, and 03F8h, in no particular order. If one of
  19. the port pointers is zero, then that indicates that a serial port does
  20. not exist there.
  21.  
  22. Any code snippets in this tutorial are compatible with Tasm 2.0
  23. assembler (but likely will work with many other versions of assemblers).
  24. They may refer to a fictional memory location known as [COMX]. [COMX] is
  25. a word variable representing anything from COM1 through COM4.
  26.  
  27. The UART
  28. --------
  29. There have been about four major functional revisions of the UART. The
  30. first one in the series was the National Semiconductor 8250.
  31.  
  32. The next version was the NS 16450. There was also an 8250A, which was
  33. functionally identical to the 16450, therefore is really just a 16450.
  34. The addition of a scratch register is what distinguishes a 16450 from
  35. the 8250.
  36.  
  37. After the 16450, came the 16550A. The 16550A added a 16 byte FIFO buffer
  38. to the data register. The FIFOs insulate the serial communications from
  39. disruptions caused by high interrupt latencies in the computer. There
  40. was also a 16550 (non-A), but that one had disfunctional FIFOs, so it
  41. looks and acts just like a 16450. From now on any references to a
  42. "16550" assumes we are talking about the 16550A revision and later.
  43.  
  44. The most recent addition to the family are the Type 3 UARTs, found in
  45. late model IBM PS/2's. The Type 3's can read and write data using the
  46. DMA circuitry of the PC. There is also a new, switchable, higher speed
  47. baud rate clock on the Type 3's, which raise maximum baud rates by 6
  48. fold.
  49.  
  50. The Data register (offset 0)
  51. ----------------------------
  52. The data register is at offset zero in the port addresses. If you read
  53. from it, it becomes the Receive Data register, and if you write to it,
  54. it becomes the Transmit Data register. The data register is eight bits
  55. long, and is common to all revisions.
  56.  
  57. If you have an UART with FIFOs (ie. 16550+ only), and they are enabled,
  58. then you do not have to stop reading or writing after doing just one.
  59. You may simply keep reading until the receiver FIFO is empty. Or you may
  60. simply keep writing until the transmitter FIFO is full. Each character
  61. will get pushed up through the FIFO stack.
  62.  
  63. The Interrupt Enable register (offset 1)
  64. ----------------------------------------
  65. At the port offset one is the Interrupt Enable register. This register
  66. controls what sorts of change of state will cause the UART to interrupt
  67. the CPU. You write to this port to set the states, and you read from
  68. this port to get the states. Only the first four bits (bits 0-3) are
  69. defined, the remainder are zero.
  70.  
  71.          7    4   3    2   1   0
  72.         ┌──────┬────┬────┬───┬───┐
  73.         │ 0000 │ MS │ LS │ T │ R │
  74.         └──────┴────┴────┴───┴───┘
  75.  
  76.         Bit 0 (R): enable Receive data ready int
  77.         Bit 1 (T): enable Transmit data empty int
  78.         Bit 2 (LS): Line Status int. Any change in the Line Status
  79.                 register with this bit set will cause an int.
  80.         Bit 3 (MS): Modem Status int. Any change in the Modem Status
  81.                 register with this bit set will cause an int.
  82.         Bits 4-7: reserved, cleared.
  83.  
  84. Sample code:
  85.         ;The following is an example of setting only the MS bit without
  86.         ;turning on any of the other bits.
  87.         ...
  88.         IER     record  ms:1,ls:1,t:1,r:1
  89.         mov     dx, [comx]              ;get comport address
  90.         inc     dx                      ;one offset from base
  91.         mov     al, IER <1,0,0,0>       ;set just MS bit
  92.         out     dx, al
  93.         ...
  94.  
  95. The Interrupt ID register (offset 2)
  96. ------------------------------------
  97. The register at this offset has a split personality depending on whether
  98. you are reading from or writing to it. If you are reading from it, then
  99. this is the Interrupt ID reg. For the 8250 & 16450, this register has
  100. only the bit fields 0-2 defined. For the 16550 and above, three
  101. additional bit fields have been defined. All undefined fields, on any
  102. particular revision, are cleared.
  103.  
  104.          7    6 5  4   3  2  1  0
  105.         ┌──────┬────┬────┬────┬───┐
  106.         │ FIFO │ 00 │ FT │ ID │ I │
  107.         └──────┴────┴────┴────┴───┘
  108.  
  109.         Bit 0 (I): Interrupt pending. Simply indicates an interrupt
  110.                 occurred, but not what the cause was (as set by
  111.                 Interrupt Enable register).
  112.         Bits 1-2 (ID): Indicates what the cause of the interrupt was.
  113.                 00b     Look in Modem Status register
  114.                 01b     Transmit data int occurred
  115.                 10b     Receive data int occurred
  116.         Bit 3 (FT): 16550+ only. FIFO Timeout. Set only in conjunction
  117.                 with ID bits = 10b (Receive data) condition. Allows the
  118.                 processor to read the last few bytes left in the FIFO
  119.                 buffer, which would otherwise be not enough to trigger a
  120.                 full receive data int.
  121.         Bits 4-5: reserved, cleared
  122.         Bits 6-7 (FIFO): 16550+ only. Mode status.
  123.                 00b     Char mode, ie. emulate 8250 or 16450.
  124.                 01b     DMA mode. Type 3 UART only.
  125.                 11b     FIFO mode.
  126.  
  127. Sample code:
  128.         ;The following is a sample interrupt handler which determines
  129.         ;if an int occurred & what sort of int occurred.
  130.         ...
  131.         IID     record  fifo:2,rsrv:2=0,ft:1,id:2,i:1
  132.         mov     dx, [comx]
  133.         add     dx, 2           ;two offsets from base
  134.         in      al, dx
  135.         test    al, mask i      ;did an int occur on this comport?
  136.         jne     no_irq          ;no? then branch
  137.         and     al, mask id     ;mask all but ID bits
  138.         cmp     al, iid <,,,01b,>;transmit data empty int?
  139.         pop     ax              ;restore AL
  140.         je      transdata       ;yes? then write to buffer
  141.         cmp     al, iid <,,,10b,>;received data int?
  142.         je      rcvdata         ;yes? then read buffer
  143.         ...
  144. rcvdata:
  145.         ...
  146. transdat:
  147.         ...
  148. no_irq:
  149.         ...
  150.  
  151. The FIFO Control register (offset 2)
  152. ------------------------------------
  153. This is the other half of the Interrupt ID register, but only available
  154. in write mode, and only available on the 16550+ UART. It is used to
  155. control various aspects of the FIFO buffers.
  156.  
  157.          7   6 5   3   2    1    0
  158.         ┌─────┬─────┬────┬────┬────┐
  159.         │ FTS │ 000 │ TR │ RR │ FE │
  160.         └─────┴─────┴────┴────┴────┘
  161.  
  162.         Bit 0 (FE): FIFO Enable
  163.         Bit 1 (RR): Receive buffer reset
  164.         Bit 2 (TR): Transmit buffer reset
  165.         Bits 3-5: reserved, cleared
  166.         Bit 6-7 (FTS): FIFO Trigger Size. Sets how full the receive
  167.                 FIFOs may get before it ints.
  168.                 00b     1 byte
  169.                 01b     4 bytes
  170.                 10b     8 bytes
  171.                 11b     14 bytes
  172.  
  173. The Line Control register (offset 3)
  174. ------------------------------------
  175. Common to all revisions. You use this register to set things like baud
  176. rates, word size, parity, etc.
  177.  
  178.           7   6    5    4   3   2  1  0
  179.         ┌───┬───┬────┬────┬───┬───┬────┐
  180.         │ D │ B │ SP │ EP │ P │ S │ WS │
  181.         └───┴───┴────┴────┴───┴───┴────┘
  182.  
  183.         Bits 0-1 (WS): Word Size. Number of data bits.
  184.                 00b     5 bits
  185.                 01b     6 bits
  186.                 10b     7 bits
  187.                 11b     8 bits
  188.         Bit 2 (S): Number of Stop bits (one or